﻿using System;
using System.Collections.Generic;
using Microsoft.WowAddonStudio.Lua.Model;

namespace Microsoft.WowAddonStudio.Lua.LanguageService.Support
{
    public class DeclarationEqualityComparer<TDeclaration> : IEqualityComparer<TDeclaration> where TDeclaration : Declaration
    {
        /// <summary>
        /// Determines whether the specified objects are equal.
        /// </summary>
        /// <param name="left">The first <see cref="Declaration"/> to compare.</param>
        /// <param name="right">The second <see cref="Declaration"/> to compare.</param>
        /// <returns>
        /// true if the specified objects are equal; otherwise, false.
        /// </returns>
        public bool Equals(TDeclaration left, TDeclaration right)
        {
            if (left == right)
                return true;
            if (left == null || right == null)
                return false;

            return Equals(left.Name, right.Name) &&
                   Equals(left.Type, right.Type);
        }

        /// <summary>
        /// Gets the hash code.
        /// </summary>
        /// <param name="declaration">The declaration.</param>
        /// <returns></returns>
        public int GetHashCode(TDeclaration declaration)
        {
            if (declaration == null)
                throw new ArgumentNullException("declaration");

            return declaration.Name.GetHashCode() + declaration.Type.GetHashCode() * 27;
        }
    }
}